package com.itheima.server.service;

import com.itheima.domain.mongo.Publish;
import com.itheima.dubbo.api.PublishApi;
import com.itheima.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 阿鹏
 * @date 2021/2/26 14:43
 */
@Service
@Slf4j
public class MovementMQService {

    @Reference
    private PublishApi publishApi;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void savePublish(String publishId) {
        sendMessage(publishId, 1);
    }

    public void watchPublish(String publishId) {
        sendMessage(publishId, 2);
    }

    public void likePublish(String publishId) {
        sendMessage(publishId, 3);
    }

    public void lovePublish(String publishId) {
        sendMessage(publishId, 4);
    }

    public void commentPublish(String publishId) {
        sendMessage(publishId, 5);
    }

    public void dislikePublish(String publishId) {
        sendMessage(publishId, 6);
    }

    public void dislovePublish(String publishId) {
        sendMessage(publishId, 7);
    }

    //1发动态；2浏览动态；3点赞；4喜欢；5评论；6取消点赞；7取消喜欢
    public void sendMessage(String publishId, Integer type) {
        Publish publish = publishApi.findById(publishId);

        Map<String, Object> message = new HashMap<>();
        message.put("userId", UserHolder.getUserId());
        message.put("publishId", publish.getPid());
        message.put("type", type);
        rocketMQTemplate.convertAndSend("tanhua-quanzi", message);

        log.info("发送消息，topic:{}, 消息内容:{}", "tanhua-quanzi", message);
    }
}
